﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;

namespace StravaUploader
{
    public partial class Login : PhoneApplicationPage
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        public Login()
        {
            InitializeComponent();
            InitializeSettings();
        }

        private void InitializeSettings()
        {
            if (settings.Contains("email"))
            {
                emailTextBox.Text = (String)settings["email"];
            }
            if (settings.Contains("password"))
            {
                passwordBox1.Password = (String)settings["password"];
            }
        }

        private void ShowAuthCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            //MessageBox.Show("Authenticated");
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            
        }

        private void submitButton_Click(object sender, RoutedEventArgs e)
        {
            StravaCommunicator.Auth(emailTextBox.Text, passwordBox1.Password);

            if (!settings.Contains("email"))
            {
                settings.Add("email", emailTextBox.Text);
            }
            if (!settings.Contains("password"))
            {
                settings.Add("password", passwordBox1.Password); 
            }

            StravaCommunicator.AddAuthCompletedHandler(new UploadStringCompletedEventHandler(ShowAuthCompleted));

        }

    }
}